package hkr.se1.budgetapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class GraphView extends View {

	private Paint p;
	private int startX;
	private int startY;
	private int radius;
	private ArrayList<Integer> colors;
	private EntriesDataSource eds;
	private List<Entry> entrys;
	private RectF rectF;
	private MainActivity activity;

	public GraphView(Context context, AttributeSet attr) {
		super(context, attr);

		this.activity = (MainActivity) context;
		this.eds = activity.EDS;

		p = new Paint();
		p.setColor(Color.BLUE);
		p.setTextSize(25);
		p.setAntiAlias(true);

		startX = 80;
		startY = 80;
		radius = 320;

		colors = new ArrayList<Integer>();
		colors.add(Color.YELLOW);
		colors.add(Color.BLUE);
		colors.add(Color.RED);
		colors.add(Color.GREEN);

		rectF = new RectF();

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.e("onDraw", "onDraw() is called...");
		entrys = eds.getAllEntries();

		float offset = 0;
		float sum = 0;
		float addFood = 0;
		float addTransportation = 0;
		float addCloths = 0;
		float addHobby = 0;

		for (int a = 0; a < entrys.size(); a++) {

			if (entrys.get(a).getCategory().equals("Food")) {
				addFood += entrys.get(a).getAmount();
				Log.e("food", "" + addFood);
				sum = sum + entrys.get(a).getAmount();
			}

			if (entrys.get(a).getCategory().equals("Transportation")) {
				addTransportation += entrys.get(a).getAmount();
				sum = sum + entrys.get(a).getAmount();
			}

			if (entrys.get(a).getCategory().equals("Clothes")) {
				addCloths += entrys.get(a).getAmount();
				sum = sum + entrys.get(a).getAmount();
			}

			if (entrys.get(a).getCategory().equals("Hobby")) {
				addHobby += entrys.get(a).getAmount();
				sum = sum + entrys.get(a).getAmount();
			}
		}

		float angle = (float) (360 / sum);

		rectF.set(startX, startY, startX + radius, startY + radius);
		float offsetAdd = 0;
		for (int i = 0; i < 4; i++) {

			p.setColor(colors.get(i));

			switch (i) {
			case 0:
				canvas.drawArc(rectF, 0, addFood * angle, true, p);
				canvas.drawText("Food", 20, 40, p);
				offsetAdd = addFood;
				break;
			case 1:
				canvas.drawArc(rectF, offset, addTransportation * angle, true,
						p);
				canvas.drawText("Transportation", 20, 80, p);
				offsetAdd = addTransportation;
				break;
			case 2:
				canvas.drawArc(rectF, offset, addCloths * angle, true, p);
				canvas.drawText("Clothes", 20, 120, p);
				offsetAdd = addCloths;

				break;
			case 3:
				canvas.drawArc(rectF, offset, addHobby * angle, true, p);
				canvas.drawText("Hobby", 20, 160, p);
				offsetAdd = addHobby;
				break;
			}
			offset += offsetAdd * angle;
		}

		canvas.save();
	}
}
